import logging
from redis import StrictRedis
from flask import Flask
from flask.ext.session import Session
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
from config import config
from logging.handlers import RotatingFileHandler

from info.utils.common import do_index_class

db = SQLAlchemy()
redis_store = None


def create_app(config_name='devlopment'):

    setup_log(config_name)

    app = Flask(__name__)

    app.add_template_filter(do_index_class, "index_class")
    app.config.from_object(config[config_name])

    db.init_app(app)

    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,port=config[config_name].REDIS_PORT)
    # CSRFProtect(app)
    Session(app)

    from info.modules.index import index_blu
    app.register_blueprint(index_blu)
    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    from info.modules.news import news_blu
    app.register_blueprint(news_blu)

    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)

    @app.after_request
    def after_request(response):

        from flask_wtf.csrf import generate_csrf
        token = generate_csrf()
        response.set_cookie('csrf_token',token)
        return response
    app.add_template_filter(do_index_class,'indexclass')

    return app



def setup_log(config_name):

    logging.basicConfig(level=config[config_name].LOG_LEVEL)

    file_log_handler = RotatingFileHandler('logs/log',maxBytes=1024 * 1024 * 100,backupCount=10)

    formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')

    file_log_handler.setFormatter(formatter)

    logging.getLogger().addHandler(file_log_handler)